package com.lgkj.ui.userinfo;

import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.AdressAdapter;
import com.lgkj.bean.meitao.AdressBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.TitleBuilder;
import com.lgkj.view.SwipeListLayout;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class AdressActivity extends BaseActivity {

    @BindView(R.id.lv_adress)
    ListView lv_main;
    private AdressBean bean;
    private AdressAdapter adressAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_adress;
    }

    @Override
    public void initView() {
        adressAdapter = new AdressAdapter(AdressActivity.this);
        lv_main.setAdapter(adressAdapter);
        lv_main.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    //当listview开始滑动时，若有item的状态为Open，则Close，然后移除
                    case SCROLL_STATE_TOUCH_SCROLL:
                        if (adressAdapter.getSets().size() > 0) {
                            for (SwipeListLayout s : adressAdapter.getSets()) {
                                s.setStatus(SwipeListLayout.Status.Close, true);
                                adressAdapter.getSets().remove(s);
                            }
                        }
                        break;

                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {

            }
        });
        new TitleBuilder(AdressActivity.this).setTitleText("我的地址").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }).setRIghtText("添加", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startsActivity(new Intent(AdressActivity.this, AdressAddActivity.class));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAdress();
    }

    @Override
    public void initData() {
        adressAdapter.setCallBack(new AdressAdapter.CallBack() {
            @Override
            public void clickAdd(int i) {
                operationAdress(i);
            }
        });
    }

    private void getAdress() {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        HttpUtils.postHttp(map, CommonConstant.getAddressList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    bean = new Gson().fromJson(String.valueOf(jsonObject), AdressBean.class);
                    if (bean != null && bean.getData() != null) {
                        adressAdapter.setDataBeans(bean.getData());
                        adressAdapter.notifyDataSetChanged();
                    }
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }
    private void operationAdress(final int position) {
        showDiaLog( "正在设置默认地址");
        Map<String, String> map = new HashMap<>();
        map.put("access_token",getToken());
        map.put("address_id", bean.getData().get(position).getId());
        String url = CommonConstant.setDefault;
        HttpUtils.postHttp(map, url, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    getAdress();
                } else {
                    toastLong(jsonObject.optString("msg"),0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }
}
